package org.blog.helper;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpUtils {

	private static final String HEADER_PRAGMA = "Pragma";

	private static final String HEADER_EXPIRES = "Expires";

	private static final String HEADER_CACHE_CONTROL = "Cache-Control";

	/**
	 * 判断某个请求是否是ajax请求
	 * 
	 * @param request
	 * @return
	 */
	public static boolean isAjax(HttpServletRequest request) {
		return Constants.AJAX_HEADER_VALUE.equals(request.getHeader(Constants.AJAX_HEADER_NAME));
	}

	/**
	 * 禁用浏览器缓存
	 * 
	 * @param response
	 */
	public static void preventCaching(HttpServletResponse response) {
		response.setHeader(HEADER_PRAGMA, "no-cache");
		response.setDateHeader(HEADER_EXPIRES, 1L);
		response.setHeader(HEADER_CACHE_CONTROL, "no-cache");
		response.addHeader(HEADER_CACHE_CONTROL, "no-store");
	}

	/**
	 * 将properties文件中的内容转化到properties对象中
	 * 
	 * @param fileName
	 * @return
	 * @throws GozapPacketException
	 */
	public static Properties getProperties(String fileName) throws BaseException {
		Properties properties = new Properties();
		InputStream is = HttpUtils.class.getClassLoader().getResourceAsStream(fileName);
		try {
			properties.load(is);
		} catch (IOException e) {
			throw new BaseException();
		}
		return properties;
	}
}
